package com.zjc.stock.strategy;

import com.zjc.stock.utils.DoubleUtil;

/**
 * 建仓加仓策略
 */
public class JiacangStrategy {
    private static double totalMany = 200000; //单次加仓大概金额

    private static double singleMany = 40000; //单次加仓大概金额
    private static double balanceMany = 0.2; //默认加仓差额

    private static double minPrice = 5.00;
    private static double maxPrice = 7.28;

    public static void main(String[] args) {
        totalManyStrategy(totalMany, maxPrice, minPrice);

//        singleManyStrategy(balanceMany, maxPrice, minPrice);
    }

    /**
     * 已知大概资金总量 单次加仓金额 最高 最低价格 求最佳加仓策略
     */
    public static void totalManyStrategy(double totalMany, double maxPrice, double minPrice) {
        double balancePrice = DoubleUtil.subtract(maxPrice, minPrice);
        int count = (int) Math.ceil(DoubleUtil.divide(totalMany, singleMany)); // 加仓次数
        double balanceMany = Math.ceil(DoubleUtil.divide(balancePrice, count) * 100.0) / 100.0;
        System.out.println("差价：" + balanceMany);

        double price = maxPrice;
        Strategy strategy1 = new Strategy(price, singleMany(price));

        strategy1.print();

        double total = strategy1.getMany();

        for (int i = 1; i < count; i++) {
            price = DoubleUtil.subtract(price, balanceMany);
            if (price < minPrice) {
                price = minPrice;
            }
            Strategy strategy = new Strategy(price, singleMany(price));
            strategy.print();
            total = DoubleUtil.add(total, strategy.getMany());
        }

        System.out.println("总金额：" + total);
    }


    /**
     * 已知加仓差价 最高 最低价格 求加仓策略
     */
    public static void singleManyStrategy(double balanceMany, double maxPrice, double minPrice) {
        double balancePrice = DoubleUtil.subtract(maxPrice, minPrice);
        int count = (int) Math.ceil(DoubleUtil.divide(balancePrice, balanceMany)); // 加仓次数

        System.out.println("差价：" + balanceMany);

        double price = maxPrice;
        Strategy strategy1 = new Strategy(price, singleMany(price));

        strategy1.print();

        double total = strategy1.getMany();

        for (int i = 1; i < count; i++) {
            price = DoubleUtil.subtract(price, balanceMany);
            if (price < minPrice) {
                price = minPrice;
            }
            Strategy strategy = new Strategy(price, singleMany(price));
            strategy.print();
            total = DoubleUtil.add(total, strategy.getMany());
        }

        System.out.println("总金额：" + total);
    }

    /**
     * 计算精确金额
     */
    public static double singleMany(double price) {
        int count = (int) Math.ceil(DoubleUtil.divide(singleMany, price));
        return DoubleUtil.multiply(count, price);
    }

    static class Strategy {
        private double price = 0.00; // 价格
        private double many = 0.00; // 金额

        public Strategy(double price, double many) {
            this.price = price;
            this.many = many;
        }

        private void print() {
            int count = (int) (many / price);
            System.out.println("股价：" + price + ";金额：" + many + ";" + count + "股");
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public double getMany() {
            return many;
        }

        public void setMany(double many) {
            this.many = many;
        }
    }
}


